<template>
  <TableView class="table">
    <template #form>
      <NSpace justify="space-between">
        <NSpace align="center">
          开始时间:
         <NDatePicker input-readonly style="width: 200rem" placeholder="请选择开始时间" clearable type="date"
            v-model:formatted-value="paginationReactive.startTime" />
          名称搜索:
          <NInput style="width: 200rem" placeholder="请输入名称" v-model:value="paginationReactive.assessName" clearable />
          考核状态:
          <NSelect style="width: 200rem" placeholder="请选择考核状态" v-model:value="paginationReactive.processStatusCode"
            clearable :options="statusList" />
          提交状态:
          <NSelect style="width: 200rem" placeholder="请选择提交状态" v-model:value="paginationReactive.submitStatusCode"
            clearable :options="submitList" />
          考核部门:
          <NSelect style="width: 200rem" placeholder="请选择审核部门" v-model:value="paginationReactive.deptId" clearable
            :options="partList" />
          <NButton type="primary" style="width: 78rem" ghost
            @click="() => ((paginationReactive.page = 1), queryTableData())">
            查询
          </NButton>
        </NSpace>
      </NSpace>
    </template>

    <template #table="props">
      <NDataTable v-bind="props" :bordered="false" :row-key="rowKey" :columns="columns" :data="tableData"
        :loading="tableLoading" />
    </template>

    <template #pagination>
        <NPagination :page-sizes="paginationReactive.pageSizes" :item-count="paginationReactive.itemCount"
        :page="paginationReactive.page" :page-size="paginationReactive.pageSize"
        @update:page="paginationReactive.onChange" @update:page-size="paginationReactive.onUpdatePageSize"
        show-size-picker show-quick-jumper :suffix="() => '页'">
        <template #prefix>
          <span style="font-size: 14px;"> 共{{ paginationReactive.itemCount }}条</span>
        </template>
      </NPagination>
    </template>
  </TableView>
</template>

<script setup lang="ts">
import TableView from '@/components/TableView.vue'
import {
  NButton,
  NDataTable,
  NPagination,
  useMessage,
  type DataTableColumns,
  NSpace,
  NInput,
  NSelect,
  NDatePicker
} from 'naive-ui'
import { h, ref, reactive, watch } from 'vue'
import router from '@/router'
import { useRoute } from 'vue-router'
import { sendRequest } from '@/utils/api'
import { useViewerStore } from '../Common/store'

const message = useMessage()
const route = useRoute()
const useStore = useViewerStore()

const rowKey: any = (e: any) => e.id || ''
const columns: DataTableColumns = [
  {
    title: '序号',
    key: 'orderIndex',
    render: (_, i) => {
      return ++i + (paginationReactive.page - 1) * paginationReactive.pageSize
    },
    width: '60',
    align: 'center'
  },
  {
    title: '考核名称',
    key: 'assessName',
    width: '120',
    align: useStore.isCenter,
    ellipsis: {
      tooltip: {
        contentStyle: {
          maxWidth: '400rem'
        }
      }
    }
  },
  {
    title: '开始时间',
    key: 'startTime',
    width: '120',
    align: useStore.isCenter
  },
  {
    title: '截止时间',
    key: 'endTime',
    width: '120',
    align: useStore.isCenter
  },
  {
    title: '考核状态',
    key: 'processStatus',
    width: '60',
    align: useStore.isCenter
  },
  {
    title: '提交状态',
    key: 'submitStatus',
    width: '60',
    align: useStore.isCenter
  },
  {
    title: '考核部门',
    key: 'deptName',
    width: '100',
    align: useStore.isCenter
  },
  {
    title: '考核人数',
    key: 'num',
    width: '60',
    align: useStore.isCenter
  },
  {
    title: '操作',
    key: 'action',
    width: '80',
    align: useStore.isCenter,
    fixed: 'right',
    render: (row: any) =>
      h(
        NButton,
        {
          type: 'primary',
          quaternary: true,
          onClick: () => {
            router.push({ path: '/OA/Examination/ApproveInfo', query: { id: row.id as never } })
          }
        },
        {
          default: () => '详情'
        }
      )
  }
]

const paginationReactive = reactive({
  page: 1,
  pageSize: 10,
  showSizePicker: true,
  pageSizes: [10, 20, 50, 100, 500],
  itemCount: 0,
  startTime: null, //开始时间
  deptId: null, //部门
  assessName: null, //考核名称
  processStatusCode: null, //考核状态
  submitStatusCode: null, //提交状态
  onChange: (page: number) => {
    paginationReactive.page = page
    queryTableData()
  },
  onUpdatePageSize: (pageSize: number) => {
    paginationReactive.pageSize = pageSize
    paginationReactive.page = 1
    queryTableData()
  }
})

//提交状态
const submitList = ref()
const getSubmitList = async () => {
  const [error, res] = await sendRequest({
    module: 'office',
    api: 'roomType',
    override: {
      params: {
        dicCode: 'A07'
      }
    }
  })
  if (error) return message.error(error.message)
  if (res.code !== 0) return message.error(res.msg)
  submitList.value = []
  res.data.map((i: any) => {
    submitList.value.push({
      label: i.dicName,
      value: i.dicCode
    })
  })
}
getSubmitList()

//考核状态
const statusList = ref()
const getStatus = async () => {
  const [error, res] = await sendRequest({
    module: 'office',
    api: 'roomType',
    override: {
      params: {
        dicCode: 'A03'
      }
    }
  })
  if (error) return message.error(error.message)
  if (res.code !== 0) return message.error(res.msg)
  statusList.value = []
  res.data.map((i: any) => {
    statusList.value.push({
      label: i.dicName,
      value: i.dicCode
    })
  })
}
getStatus()

//审核部门
const partList: any = ref([])
const getPartName = async () => {
  const [error, res] = await sendRequest({
    module: 'office',
    api: 'partList',
    override: {
      params: {
        page: 1,
        limit: 9999999
      }
    }
  })
  if (error) return message.error(error.message)
  if (res.code !== 0) return message.error(res.msg)
  partList.value = []
  const tmp: any = res.data
  tmp[0].children.map((i: any) => {
    partList.value.push({
      label: i.name,
      value: i.id
    })
  })
}
getPartName()

//表格数据
const tableLoading = ref(false)
const tableData: any = ref([])
const queryTableData = async (resetPage = false) => {
  if (resetPage) paginationReactive.page = 1
  tableLoading.value = true
  const [error, res] = await sendRequest({
    module: 'exam',
    api: 'approveList',
    override: {
      params: {
        page: paginationReactive.page,
        limit: paginationReactive.pageSize,
        startTime: paginationReactive.startTime,
        deptId: paginationReactive.deptId,
        assessName: paginationReactive.assessName,
        processStatusCode: paginationReactive.processStatusCode,
        submitStatusCode: paginationReactive.submitStatusCode
      }
    }
  })
  tableLoading.value = false
  if (error) return message.error(error.message)
  paginationReactive.itemCount = res.data?.totalCount
  tableData.value = res.data.list
}

watch(
  () => route.path,
  () => {
    if (route.path == '/OA/Examination/ApproveExamination') queryTableData()
  },
  { deep: true, immediate: true }
)
</script>

<style scoped lang="scss">
.table {
  height: 100%;
  padding: 29px 30px 42px;
}
</style>
